package edu.mcw.rgd.clinminer.web;

import java.util.Calendar;

import javax.annotation.PostConstruct;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.vaadin.browsercookies.BrowserCookies;

import com.vaadin.Application;

@Configurable
@Component
@Scope(WebApplicationContext.SCOPE_SESSION)
public class ClinminerApplication extends Application {
	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger
			.getLogger(ClinminerApplication.class);

	private ClinminerWindow mainWindow;

	// BrowserCookies widget (by default reads the cookie values)
	private BrowserCookies cookies;

	@Value("${application.version}")
	private String version;

	public ClinminerApplication() {
		cookies = new BrowserCookies();

		mainWindow = new ClinminerWindow();

		setMainWindow(mainWindow);
	}

	@Override
	@PostConstruct
	public void init() {
	}

	@Override
	public String getVersion() {
		return version;
	}

	public String getCookie(String cookieName) {
		return cookies.getCookies().get(cookieName);
	}

	public void setCookie(String cookieName, String value) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.YEAR, 20);
		cookies.setCookie(cookieName, value, c.getTime());
	}

	public BrowserCookies getCookies() {
		return cookies;
	}
}
